package cn.yshujia.controller;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Website;
import cn.yshujia.domain.vo.ApiVO;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.domain.vo.WebsiteVO;
import cn.yshujia.service.impl.WebsiteServiceImpl;
import com.alibaba.fastjson2.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 控制器
 */

@RestController
@Tag (name = "Friends", description = "友链Api")
public class WebsiteController {
	
	@Resource
	private WebsiteServiceImpl service;
	
	@GetMapping ("/website/page")
	@Operation (summary = "获取友链分页, 通过分页中的参数区分是否推荐")
	public ApiVO<PageVO<WebsiteVO>> list(@RequestParam String dto) {
		PageDTO dtoPage = JSON.parseObject(dto, PageDTO.class);
		if (null == dtoPage) {
			return ApiVO.error();
		}
		return ApiVO.success(service.page(dtoPage));
	}
	
	@GetMapping ("/admin/website/page")
	@Operation (summary = "admin 获取友链分页")
	@PreAuthorize ("@sys.hasOnePermission('website:all')")
	public ApiVO<PageVO<WebsiteVO>> page(@RequestParam String dto) {
		PageDTO dtoPage = JSON.parseObject(dto, PageDTO.class);
		if (null == dtoPage) {
			return ApiVO.error();
		}
		return ApiVO.success(service.adminPage(dtoPage));
	}
	
	@PostMapping ("/website/insert")
	@Operation (summary = "添加友链")
	@PreAuthorize ("@sys.hasOnePermission('website:all', 'website:insert')")
	public ApiVO<Boolean> insert(@RequestBody Website dto) {
		Boolean result = service.insert(dto);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@PutMapping ("/website/update")
	@Operation (summary = "更新友链")
	@PreAuthorize ("@sys.hasOnePermission('website:all', 'website:update')")
	public ApiVO<Boolean> update(@RequestBody Website dto) {
		Boolean result = service.update(dto);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@DeleteMapping ("/website/delete")
	@Operation (summary = "删除友链")
	@PreAuthorize ("@sys.hasOnePermission('website:all', 'website:delete')")
	public ApiVO<Boolean> delete(@RequestBody List<Long> ids) {
		Boolean result = service.remove(ids);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
}
